package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.TAEnrollment;
import com.eece496.Database.TAduties;
import com.eece496.Helpers.TADutiesInfo;
import com.eece496.Helpers.TAInfo;

/**
 * Servlet implementation class ProfTADutyBallotServlet
 */
public class ProfTADutyBallotServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProfTADutyBallotServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Connect();

		String ButtonClicked = request.getParameter("button");
		String String1 = request.getParameter("courseNumber");
		String courseNumber = String1.substring(0, 8);
		String sectionNumber = String1.substring(9, 12);
		String division = String1.substring(13, 14);
		String COGSName = String1.substring(15);

		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");

		System.out.println(division);
		System.out.println(COGSName);

		if (ButtonClicked.equals("Add TA Duties")) {
			TAEnrollment TAEnrollmentDB = new TAEnrollment();
			TAEnrollmentDB.Setup(connection);

			ArrayList<TAInfo> info = new ArrayList<TAInfo>();
			try {
				info = TAEnrollmentDB.GetAllTAsEnrolled(courseNumber,
						sectionNumber);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			request.setAttribute("TAInfo", info);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("division", division);
			request.setAttribute("COGSName", COGSName);
			request.getRequestDispatcher(
					"/Professor/Professor-Add-TADuties.jsp").forward(request,
					response);
		}
		else if(ButtonClicked.equals("View TA Duties"))
		{
			TAduties TADutyDB = new TAduties();
			TADutyDB.Setup(connection);
			
			ArrayList<TADutiesInfo> info = new ArrayList<TADutiesInfo>();
			
			try {
				info = TADutyDB.GetAllTADutiesByDivision(courseNumber, sectionNumber, division);
				
				
				request.setAttribute("TADutyInfo", info);
				request.setAttribute("firstname", firstname);
				request.setAttribute("lastname", lastname);
				request.setAttribute("courseNumber", courseNumber);
				request.setAttribute("sectionNumber", sectionNumber);
				request.setAttribute("division", division);
				request.setAttribute("COGSName", COGSName);
				
				request.getRequestDispatcher(
						"/Professor/Professor-View-TADuties.jsp").forward(request,
						response);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}

	}

}
